function tree(){}
tree.prototype={
	init:function(){
		cleanContent($('workTd'));//清除内容
		$('quotaTree').style.display='';
		cleanContent($('treeRoot'));
		$('treeRoot').appendChild($C('div'),{});//为了后面的代码 return n.lastChild;
		this.makeTree();//生成树形状
		this.artisticTree($('treeRoot'));//美化生成的树
		this.makeAttTable();//生成一个表格，显示节点属性的
	},
 	makeTree:function(){
		var pNode;
		for(var t in QUOTATREE){
			(new treeNode((!QUOTATREE[t].item),t,'<span class="green">'+t.replace('T','')+' </span>'+QUOTATREE[t].name)).init();
		}
	},
	artisticTree:function(r){//使生成的树看起来更美观
		var n=r.childNodes;
		var l=n.length,divNodes=[];//所有是div的node
		for(var i=0;i<l;i++){
			if('div'==n[i].nodeName.toLowerCase())	divNodes.push(n[i]);//如果是div标签，则push到divNode
		}
		var lj=divNodes.length;
		if(0==lj)	return;//没有div标签了
		removeClass(divNodes[lj-1],'treebg');//去掉最后一个节点的虚线
		for(var j=0;j<lj;j++){
			this.artisticTree(divNodes[j]);
		}
	},
	makeAttTable:function(){
		$('workTd').appendChild($C('div',{'class':'innerDiv'},$C('table',{'width':'100%','border':'0','class':'attTable'},$C('thead',{},$C('tr',{},$C('th',{'class':'cen','width':'35%'},'属性名称 '),$C('th',{'class':'cen','width':'75%'},'属性值'))),$C('tbody',{'id':'attrList'}))));
	}
};
function treeNode(s,rank,nName){//s 是 status，展开还是合拢状态
	this.s=s;
	this.rank=rank.split('-');
	this.nName=nName;
	this.budgetNo=rank;
	this.treeId;
}
treeNode.prototype={
	attrNames:{'index':'序号','name':'名称','content':'内容','unit':'单位','workinghour':'工作周期','item':false},
	plusIcon:'images/plus.gif',//加号图标
	minusIcon:'images/minus.gif',//减号图标
 	joinIcon:'images/join.gif',//连接图标
	openBook:'images/book_open.gif',//打开的书
	closeBook:'images/mana_tree.gif',//关闭的书
	itemIcon:'images/item.gif',//每一项
	init:function(){
		this.complete=(1==this.s)?true:false;//如果前面是+号，说明没有加载完，那么在第一次点击的时候就要加载
		this.getParent($('treeRoot')).appendChild(this.makeNode());
	},
 	getParent:function(n){
		this.treeId=this.rank.shift();//把头元素移出
		if(0==this.rank.length)	return n.lastChild;
		var divs=n.lastChild.childNodes;
		var l=divs.length;
		for(var i=0;i<l;i++){
			//debug(divs[i].treeId);
			if(this.treeId==divs[i].treeId)	return this.getParent(divs[i]);
		}
		return n;//如果没有找到。
	},
 	makeNode:function(){
		this.iconImg=$C('img',{});
		this.bookImg=$C('img',{});
		this.setIcon();
		if(2!=this.s){
			addClass(this.iconImg,'click');
			addClass(this.bookImg,'click');
			eve.observe(this.iconImg,'click',this.clickIcon.bind(this));
			eve.observe(this.bookImg,'click',this.clickIcon.bind(this));
		}
		var span=$C('span',{'class':'click'},this.nName);
		eve.observe(span,'click',this.showDetail.bind(this));
		this.tNode=$C('div',{'class':'node treebg'},this.iconImg,this.bookImg,span,$C('div',{}));
		this.tNode.treeId=this.treeId;
		return this.tNode;
	},
	getBudgetNo:function(){
		//return (2==this.s)?bNo=this.budgetNo+'-'+this.treeId:this.budgetNo.replace('T','');
		return this.budgetNo.replace('T','');
	},
	showDetail:function(){
		attrTable=$('attrList');
		cleanContent(attrTable);
		var attName,tr,items;
		//var bNo=
		items=this.getItems(this.budgetNo,this.treeId);
		for(var a in items){
			//debug(a);
			attName=this.attrNames[a];
			if(false==attName)	continue;
			var content=('index'==a)?this.getBudgetNo():items[a];
			//debug(attName+'  '+QUOTATREE[this.budgetNo][a]);
			attrTable.appendChild($C('tr',{},$C('td',{'class':'cen'},attName),$C('td',{},content)));
		}
		if(false==isAdmin())	return;
		var editSpan=$C('span',{'class':'editAttr click'},'[修改]');
		eve.observe(editSpan,'click',this.editAttr.bind(this));
		attrTable.appendChild($C('tr',{},$C('td',{'class':'cen'},'操作'),$C('td',{},editSpan)));
	},
	editAttr:function(){
		var mForm=$C('ul',{}),attName;
		var items=this.getItems(this.budgetNo,this.treeId);
		var tNo=this.getBudgetNo();
		for(var a in items){
			//if('index'==a)	continue;
			attName=this.attrNames[a];
			if(false==attName)	continue;
			var obj=('index'==a)?$C('span',{},tNo):$C('input',{'type':'text','size':'30','name':a,'value':items[a]});//序号不能改
			mForm.appendChild($C('li',{'class':'l'},attName));
			mForm.appendChild($C('li',{'class':'r'},obj));
		}
		var okBtn=$C('input',{'type':'button','class':'btn','value':'确定'});
		var closeBtn=$C('input',{'type':'button','class':'btn','value':'取消'});
		eve.observe(okBtn,'click',function(){
			if(false==confirm('定额不能轻易改动,你确定要继续操作吗?'))	return	w();
			var dataObj={};
			var input=mForm.getElementsByTagName('input');
			for(var i=0;i<input.length;i++){
				if(''==input[i].value)	return alert(this.attrNames[input[i].name]+'的值不能为空');
				dataObj[input[i].name]=input[i].value;
			}
			ajax.post('utils/quota.php?action=modifybudget',postParam('quo',{'budgetno':tNo,'data':dataObj}),function(result){
				var re=parseJson(result);
				if(1==re.result){
					w();//关闭窗口
					manageQuota();//重新加载定额
					psbirdDialog.alert('定额已经修改,但不会立刻生产效果.下一次加载的时候才会生效.<br/><br/>如果想立刻生效,刷新网页.');
				//	showStatus('定额修改成功');
				}
			});
		});
		var w=psbirdDialog.html($C('div',{},$C('div',{'class':'modifyForm'},mForm),$C('div',{'class':'clear'}),$C('div',{'class':'cen'},okBtn,closeBtn)),400,'编辑定额');
		eve.observe(closeBtn,'click',w);
	},
	clickIcon:function(){
		if(false==this.complete){
			this.continueLoad();//如果没有加载完，点第一次的时候继续加载
			this.complete=true;
		}
		if(1==this.s)
			addClass(this.tNode.lastChild,'hide');
		else
			removeClass(this.tNode.lastChild,'hide');
		this.s=Math.abs(this.s-1);//0 和 1 互换
		this.setIcon();
	},
	getItems:function(budgetNo,treeId){
		if(2!=this.s)
			return QUOTATREE[this.budgetNo];
		budgetNo=budgetNo.replace(/^(.*)-\d$/,'$1');
		tmpItem=QUOTATREE[budgetNo]['item'];
		//debug(tmpItem.length);
		for(var i=0;i<tmpItem.length;i++){
			if(tmpItem[i].index==treeId)	return tmpItem[i];
		}
	},
	continueLoad:function(){
		var items=QUOTATREE[this.budgetNo].item;
		var l=items.length;
		for(var i=0;i<l;i++){
			(new treeNode(2,this.budgetNo+'-'+items[i].index,'<span class="green">'+items[i].index+' </span>'+items[i].name)).init();
		}
		removeClass(this.tNode.lastChild.lastChild,'treebg');
	},
	getIconPath:function(){//取得最前面的最标的地址
		if(1==this.s)	return this.minusIcon;
		else if(0==this.s)	return this.plusIcon;
		else if(2==this.s)	return this.joinIcon;
	},
	getBookImgPath:function(){//取得书图标的状态
		if(1==this.s)	return this.openBook;
		else if(0==this.s)	return this.closeBook;
		else if(2==this.s)	return this.itemIcon;		
	},
	setIcon:function(){
		this.iconImg.src=this.getIconPath();
		this.bookImg.src=this.getBookImgPath();
	}
};